package com.apes.scm.settle.payment.model;

import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.Model;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.plugin.soe.metadata.BusinessAttributeValue;
import com.apes.framework.util.ApesConst;
import com.apes.scm.masterdata.party.model.PartyRole;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;

import javax.persistence.*;
import javax.validation.constraints.NotNull;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = Payment.CONST_TABLE_NAME)
public class Payment extends Model {
    public static final String CONST_TABLE_NAME = "acc_payment";

    @Id
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "付款表", padding = 5, prefix = "FF%(company)%(yy)%(month)", useDateRange = true)
    @Comment("单号")
    private String id; //付款单号

    @States(name = "付款类型", values = {
            @State(id = "invoice_payment", name = "发票付款单"),
            @State(id = "pre_payment", name = "预付款")})
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "payment_type", nullable = false, referencedColumnName = "id")
    private BusinessAttributeValue paymentType; //付款类型

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "company_id", referencedColumnName = "id")
    @Comment("公司编码")
    private PartyRole company; //公司

    @NotNull
    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "partner_id", referencedColumnName = "id", nullable = false)
    @Comment("业务伙伴编码")
    private PartyRole partner; //业务伙伴

    @States(name = "付款状态", values = {
            @State(id = "sent", name = "付款申请"),
            @State(id = "approve", name = "付款审核"),
            @State(id = "executive", name = "已付款")})
    @Column(name = "state", length = 10, nullable = false)
    private String state; //状态

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "approve_uid", referencedColumnName = "id")
    @Comment("审核人")
    private PartyRole approveUID; //审核

    @Column(name = "approve_date")
    @Comment("审核日期")
    private Date approveDate; //审核日期

    @ManyToOne(fetch = FetchType.LAZY)
    @JoinColumn(name = "delete_uid", referencedColumnName = "id")
    @Comment("删除人")
    private PartyRole deleteUID; //删除

    @Column(name = "delete_date")
    @Comment("删除日期")
    private Date deleteDate; //删除日期

    @Column(name = "note", length = 250)
    @Comment("备注")
    private String note; //备注

    @Column(name = "amount", nullable = false, columnDefinition = ApesConst.FIELD_16_4)
    @Comment("付款金额")
    private double amount = 0; //付款金额

    @Column(name = "source", length = 30)
    @Comment("源单据号码")
    private String source;

    @Column(name = "source_business_type", length = 32)
    private String sourceBusinessType; // 源单据类型

    @Comment("付款回执")
    @Column(name = "file_address", length = 4000)
    private String fileAddres;

    /**
     * 计算明细金额
     */
    public void computeAmt() {
        amount = 0;
        for (PaymentItem item : paymentItemSet) {
            amount += item.getAmount();
        }
    }

    @OneToMany(mappedBy = "payment", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<PaymentItem> paymentItemSet = new HashSet(); //付款单明细

    public Set<PaymentItem> getPaymentItemSet() {
        if (paymentItemSet == null || paymentItemSet.isEmpty()) return new HashSet<>();
        for (PaymentItem item : paymentItemSet) {
            item.setPayment(this);
        }
        return paymentItemSet;
    }

    @OneToMany(mappedBy = "payment", cascade = CascadeType.ALL, orphanRemoval = true)
    private Set<PaymentMethod> paymentMethodSet = new HashSet(); // 付款方式明细

    public Set<PaymentMethod> getPaymentMethodSet() {
        if (paymentMethodSet == null) {
            paymentMethodSet = new HashSet();
        }
        for (PaymentMethod item : paymentMethodSet) {
            item.setPayment(this);
        }
        return paymentMethodSet;
    }
}
